<?php

class logger {

    private static $__log_levels = array(
        LOG_SYS_EMERG => 'EMERG',
        LOG_SYS_ALERT => 'ALERT',
        LOG_SYS_CRIT => 'CRIT',
        LOG_SYS_ERR => 'ERR',
        LOG_SYS_WARNING => 'WARNING',
        LOG_SYS_NOTICE => 'NOTICE',
        LOG_SYS_INFO => 'INFO',
        LOG_SYS_DEBUG => 'DEBUG'
    );

    private static $__log_id = null;

    private static $__log_level = null;

    private static $__default_log_level = LOG_SYS_INFO;

    static public function __init() {
        if (self::$__log_level === null) {
            if (defined('LOG_LEVEL') && array_key_exists(LOG_LEVEL, self::$__log_levels)) {
                self::$__log_level = LOG_LEVEL;
            } else {
                self::$__log_level = self::$__default_log_level;
            }
            
            self::$__log_id = $_SERVER["REQUEST_TIME"] . uniqid();
        }
    }

    static public function get_log_level() {
        self::__init();
        self::$__log_level;
    }

    static public function emerg($message) {
        logger::log($message, LOG_SYS_EMERG);
    }

    static public function alert($message) {
        logger::log($message, LOG_SYS_ALERT);
    }

    static public function crit($message) {
        logger::log($message, LOG_SYS_CRIT);
    }

    static public function error($message) {
        logger::log($message, LOG_SYS_ERR);
    }

    static public function warning($message) {
        logger::log($message, LOG_SYS_WARNING);
    }

    static public function notice($message) {
        logger::log($message, LOG_SYS_NOTICE);
    }

    static public function info($message) {
        logger::log($message, LOG_SYS_INFO);
    }

    static public function debug($message) {
        logger::log($message, LOG_SYS_DEBUG);
    }

    static public function log($message, $log_level = LOG_SYS_INFO) {
        self::__init();
        if (kernel::$console_output) {
            if ($log_level < LOG_SYS_DEBUG) {
                echo $message = $message . "\n";
            }
        }
        
        if ($log_level <= self::$__log_level) {
            $message = sprintf("%s\t%s\t%s\n", date("Y-m-d H:i:s") . "\t" . self::$__log_id, self::$__log_levels[$log_level], $message);
            switch (LOG_TYPE) {
                case 3:
                    if (defined('LOG_FILE')) {
                        $logfile = str_replace('{date}', date("Ymd"), LOG_FILE);
                        $ip = ($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '127.0.0.1';
                        $ip = str_replace(array(
                            '.',
                            ':'
                        ), array(
                            '_',
                            '_'
                        ), $ip);
                        $logfile = str_replace('{ip}', $ip, $logfile);
                    } else {
                        $logfile = DATA_DIR . '/logs/all.php';
                    }
                    if (! file_exists($logfile)) {
                        if (! is_dir(dirname($logfile)))
                            utils::mkdir_p(dirname($logfile));
                        file_put_contents($logfile, (defined(LOG_HEAD_TEXT)) ? LOG_HEAD_TEXT : '<' . '?php exit()?' . ">\n");
                    }
                    @error_log($message, 3, $logfile);
                    break;
                case 2:
                    @error_log($message, 0);
                case 0:
                default:
                    @syslog($log_level, $message);
            } // End Switch
        }
    }

    static function appned() {}
}
